import os
import polars as pl
from sqlite.sqlite_database import SQLite


base_path = None
recipient_email = None
pdf_path = None
db_path = None
MaximumAttachmentSize = 0
excel_path = None
BOOKING_FORM_template_path = None
sqlite = None
shipper_path=None
config_df = None
png_path = None


def set_base_path(path):
    global base_path
    global recipient_email
    global pdf_path
    global db_path
    global MaximumAttachmentSize
    global excel_path
    global BOOKING_FORM_template_path
    global sqlite
    global shipper_path
    base_path = path
    global config_df
    global png_path

    # 读取配置文件
    config_df = (
        pl.read_excel(
            os.path.join('D:/配置文件.xlsx'),
            sheet_name=["air","sea","si"],
            columns=["表名", "原字段名", "现字段名"],
        )
    )
    # 读取其它配置文件
    config_other_df = (
        pl.read_excel(
            os.path.join('D:/配置文件.xlsx'),
            sheet_name="其它配置",
            columns=["key", "value"],
            schema_overrides={"value": pl.String},  # 覆盖默认的列类型
        )
        .fill_null('')
    )

    recipient_email = config_other_df.filter(pl.col("key")=="recipient_email")["value"].item(0) # 收件人邮箱
    pdf_path = os.path.abspath(config_other_df.filter(pl.col("key")=="pdf_path")["value"].item(0)) # pdf共享盘路径
    db_path = os.path.abspath(config_other_df.filter(pl.col("key")=="db_path")["value"].item(0)) # sqlite文件路径
    MaximumAttachmentSize = int(config_other_df.filter(pl.col("key")=="MaximumAttachmentSize")["value"].item(0)) | 0 # 附件大小限制
    excel_path= os.path.abspath(config_other_df.filter(pl.col("key")=="excel_path")["value"].item(0)) # excel附件保存文件路径
    BOOKING_FORM_template_path = os.path.abspath(config_other_df.filter(pl.col("key")=="BOOKING_FORM_template_path")["value"].item(0)) # BOOKING FORM模板路径
    shipper_path= os.path.abspath(config_other_df.filter(pl.col("key")=="shipper_path")["value"].item(0)) # Shipper基表路径
    sqlite = SQLite(db_path) # 实例化SQLite类
    png_path=os.path.join(path, 'F类出口合规申报预警通知.png')